2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Revision\MutableRevisionRecord
;
4 use MediaWiki\Revision\RevisionStore
;
5 use MediaWiki\Revision\IncompleteRevisionException
;
6 use MediaWiki\Revision\RevisionRecord
;
7 use MediaWiki\Revision\SlotRecord
;
10 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
15 abstract class RevisionDbTestBase
extends MediaWikiTestCase
{
18 * @var WikiPage $testPage
22 public function __construct( $name = null, array $data = [], $dataName = '' ) {
23 parent
::__construct( $name, $data, $dataName );
25 $this->tablesUsed
= array_merge( $this->tablesUsed
,
48 protected function addCoreDBData() {
49 // Blank out. This would fail with a modified schema, and we don't need it.
55 abstract protected function getMcrMigrationStage();
60 abstract protected function getMcrTablesToReset();
62 protected function setUp() {
63 $this->tablesUsed +
= $this->getMcrTablesToReset();
67 $this->mergeMwGlobalArrayValue(
71 12313 => 'Dummy_talk',
75 $this->mergeMwGlobalArrayValue(
76 'wgNamespaceContentModels',
78 12312 => DummyContentForTesting
::MODEL_ID
,
82 $this->mergeMwGlobalArrayValue(
85 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
86 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
90 $this->setMwGlobals( [
91 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
92 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
93 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD
,
96 $this->overrideMwServices();
98 if ( !$this->testPage
) {
100 * We have to create a new page for each subclass as the page creation may result
101 * in different DB fields being filled based on configuration.
103 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
108 * @param string $model
111 protected function getMockTitle() {
112 $mock = $this->getMockBuilder( Title
::class )
113 ->disableOriginalConstructor()
115 $mock->expects( $this->any() )
116 ->method( 'getNamespace' )
117 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
118 $mock->expects( $this->any() )
119 ->method( 'getPrefixedText' )
120 ->will( $this->returnValue( __CLASS__
) );
121 $mock->expects( $this->any() )
122 ->method( 'getDBkey' )
123 ->will( $this->returnValue( __CLASS__
) );
124 $mock->expects( $this->any() )
125 ->method( 'getArticleID' )
126 ->will( $this->returnValue( 23 ) );
131 abstract protected function getContentHandlerUseDB();
133 private function makeRevisionWithProps( $props = null ) {
134 if ( $props === null ) {
138 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
139 $props['text'] = 'Lorem Ipsum';
142 if ( !isset( $props['user_text'] ) ) {
143 $user = $this->getTestUser()->getUser();
144 $props['user_text'] = $user->getName();
145 $props['user'] = $user->getId();
148 if ( !isset( $props['user'] ) ) {
152 if ( !isset( $props['comment'] ) ) {
153 $props['comment'] = 'just a test';
156 if ( !isset( $props['page'] ) ) {
157 $props['page'] = $this->testPage
->getId();
160 if ( !isset( $props['content_model'] ) ) {
161 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
164 $rev = new Revision( $props );
166 $dbw = wfGetDB( DB_MASTER
);
167 $rev->insertOn( $dbw );
173 * @param string $titleString
174 * @param string $text
175 * @param string|null $model
179 private function createPage( $titleString, $text, $model = null ) {
180 if ( !preg_match( '/:/', $titleString ) &&
181 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
183 $ns = $this->getDefaultWikitextNS();
184 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
187 $title = Title
::newFromText( $titleString );
188 $wikipage = new WikiPage( $title );
190 // Delete the article if it already exists
191 if ( $wikipage->exists() ) {
192 $wikipage->doDeleteArticle( "done" );
195 $content = ContentHandler
::makeContent( $text, $title, $model );
196 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
201 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
202 $this->assertNotNull( $rev, 'missing revision' );
204 $this->assertEquals( $orig->getId(), $rev->getId() );
205 $this->assertEquals( $orig->getPage(), $rev->getPage() );
206 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
207 $this->assertEquals( $orig->getUser(), $rev->getUser() );
208 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
209 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
210 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
214 * @covers Revision::getRecentChange
216 public function testGetRecentChange() {
217 $rev = $this->testPage
->getRevision();
218 $recentChange = $rev->getRecentChange();
220 // Make sure various attributes look right / the correct entry has been retrieved.
221 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
223 $rev->getTitle()->getNamespace(),
224 $recentChange->getAttribute( 'rc_namespace' )
227 $rev->getTitle()->getDBkey(),
228 $recentChange->getAttribute( 'rc_title' )
230 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
231 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
232 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
233 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
234 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
238 * @covers Revision::insertOn
240 public function testInsertOn_success() {
241 $parentId = $this->testPage
->getLatest();
243 // If an ExternalStore is set don't use it.
244 $this->setMwGlobals( 'wgDefaultExternalStore', false );
246 $rev = new Revision( [
247 'page' => $this->testPage
->getId(),
248 'title' => $this->testPage
->getTitle(),
249 'text' => 'Revision Text',
250 'comment' => 'Revision comment',
253 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
255 $this->assertInternalType( 'integer', $revId );
256 $this->assertSame( $revId, $rev->getId() );
258 // getTextId() must be an int!
259 $this->assertInternalType( 'integer', $rev->getTextId() );
261 $mainSlot = $rev->getRevisionRecord()->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
263 // we currently only support storage in the text table
264 $textId = MediaWikiServices
::getInstance()
266 ->getTextIdFromAddress( $mainSlot->getAddress() );
270 [ 'old_id', 'old_text' ],
272 [ [ strval( $textId ), 'Revision Text' ] ]
285 "rev_id = {$rev->getId()}",
287 strval( $rev->getId() ),
288 strval( $this->testPage
->getId() ),
293 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
298 public function provideInsertOn_exceptionOnIncomplete() {
299 $content = new TextContent( '' );
300 $user = User
::newFromName( 'Foo' );
302 yield
'no parent' => [
304 'content' => $content,
308 IncompleteRevisionException
::class,
309 "rev_page field must not be 0!"
312 yield
'no comment' => [
314 'content' => $content,
318 IncompleteRevisionException
::class,
319 "comment must not be NULL!"
322 yield
'no content' => [
328 IncompleteRevisionException
::class,
329 "Uninitialized field: content_address" // XXX: message may change
334 * @dataProvider provideInsertOn_exceptionOnIncomplete
335 * @covers Revision::insertOn
337 public function testInsertOn_exceptionOnIncomplete( $array, $expException, $expMessage ) {
338 // If an ExternalStore is set don't use it.
339 $this->setMwGlobals( 'wgDefaultExternalStore', false );
340 $this->setExpectedException( $expException, $expMessage );
342 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
343 $rev = new Revision( $array, 0, $title );
345 $rev->insertOn( wfGetDB( DB_MASTER
) );
349 * @covers Revision::newFromTitle
351 public function testNewFromTitle_withoutId() {
352 $latestRevId = $this->testPage
->getLatest();
354 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
356 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
357 $this->assertEquals( $latestRevId, $rev->getId() );
361 * @covers Revision::newFromTitle
363 public function testNewFromTitle_withId() {
364 $latestRevId = $this->testPage
->getLatest();
366 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
368 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
369 $this->assertEquals( $latestRevId, $rev->getId() );
373 * @covers Revision::newFromTitle
375 public function testNewFromTitle_withBadId() {
376 $latestRevId = $this->testPage
->getLatest();
378 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
380 $this->assertNull( $rev );
384 * @covers Revision::newFromRow
386 public function testNewFromRow() {
387 $orig = $this->makeRevisionWithProps();
389 $dbr = wfGetDB( DB_REPLICA
);
390 $revQuery = Revision
::getQueryInfo();
391 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
392 __METHOD__
, [], $revQuery['joins'] );
393 $this->assertTrue( is_object( $res ), 'query failed' );
395 $row = $res->fetchObject();
398 $rev = Revision
::newFromRow( $row );
400 $this->assertRevEquals( $orig, $rev );
403 public function provideNewFromArchiveRow() {
411 return $f +
[ 'ar_namespace', 'ar_title' ];
416 unset( $f['ar_text_id'] );
422 unset( $f['ar_page_id'] );
428 unset( $f['ar_parent_id'] );
434 unset( $f['ar_rev_id'] );
440 unset( $f['ar_sha1'] );
447 * @dataProvider provideNewFromArchiveRow
448 * @covers Revision::newFromArchiveRow
450 public function testNewFromArchiveRow( $selectModifier ) {
451 $services = MediaWikiServices
::getInstance();
453 $store = new RevisionStore(
454 $services->getDBLoadBalancer(),
455 $services->getService( '_SqlBlobStore' ),
456 $services->getMainWANObjectCache(),
457 $services->getCommentStore(),
458 $services->getContentModelStore(),
459 $services->getSlotRoleStore(),
460 $services->getSlotRoleRegistry(),
461 $this->getMcrMigrationStage(),
462 $services->getActorMigration()
465 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
466 $this->setService( 'RevisionStore', $store );
468 $page = $this->createPage(
469 'RevisionStorageTest_testNewFromArchiveRow',
471 CONTENT_MODEL_WIKITEXT
473 $orig = $page->getRevision();
474 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
476 $dbr = wfGetDB( DB_REPLICA
);
477 $arQuery = Revision
::getArchiveQueryInfo();
478 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
480 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
481 __METHOD__
, [], $arQuery['joins']
483 $this->assertTrue( is_object( $res ), 'query failed' );
485 $row = $res->fetchObject();
488 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
489 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
490 $rev = Revision
::newFromArchiveRow( $row );
492 $this->assertRevEquals( $orig, $rev );
496 * @covers Revision::newFromArchiveRow
498 public function testNewFromArchiveRowOverrides() {
499 $page = $this->createPage(
500 'RevisionStorageTest_testNewFromArchiveRow',
502 CONTENT_MODEL_WIKITEXT
504 $orig = $page->getRevision();
505 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
507 $dbr = wfGetDB( DB_REPLICA
);
508 $arQuery = Revision
::getArchiveQueryInfo();
510 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
511 __METHOD__
, [], $arQuery['joins']
513 $this->assertTrue( is_object( $res ), 'query failed' );
515 $row = $res->fetchObject();
518 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
520 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
521 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
525 * @covers Revision::newFromId
527 public function testNewFromId() {
528 $orig = $this->testPage
->getRevision();
529 $rev = Revision
::newFromId( $orig->getId() );
530 $this->assertRevEquals( $orig, $rev );
534 * @covers Revision::newFromPageId
536 public function testNewFromPageId() {
537 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
538 $this->assertRevEquals(
539 $this->testPage
->getRevision(),
545 * @covers Revision::newFromPageId
547 public function testNewFromPageIdWithLatestId() {
548 $rev = Revision
::newFromPageId(
549 $this->testPage
->getId(),
550 $this->testPage
->getLatest()
552 $this->assertRevEquals(
553 $this->testPage
->getRevision(),
559 * @covers Revision::newFromPageId
561 public function testNewFromPageIdWithNotLatestId() {
562 $content = new WikitextContent( __METHOD__
);
563 $this->testPage
->doEditContent( $content, __METHOD__
);
564 $rev = Revision
::newFromPageId(
565 $this->testPage
->getId(),
566 $this->testPage
->getRevision()->getPrevious()->getId()
568 $this->assertRevEquals(
569 $this->testPage
->getRevision()->getPrevious(),
575 * @covers Revision::getPage
577 public function testGetPage() {
578 $page = $this->testPage
;
580 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
581 $rev = Revision
::newFromId( $orig->getId() );
583 $this->assertEquals( $page->getId(), $rev->getPage() );
587 * @covers Revision::isCurrent
589 public function testIsCurrent() {
590 $rev1 = $this->testPage
->getRevision();
592 # @todo find out if this should be true
593 # $this->assertTrue( $rev1->isCurrent() );
595 $rev1x = Revision
::newFromId( $rev1->getId() );
596 $this->assertTrue( $rev1x->isCurrent() );
598 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
599 $rev2 = $this->testPage
->getRevision();
601 # @todo find out if this should be true
602 # $this->assertTrue( $rev2->isCurrent() );
604 $rev1x = Revision
::newFromId( $rev1->getId() );
605 $this->assertFalse( $rev1x->isCurrent() );
607 $rev2x = Revision
::newFromId( $rev2->getId() );
608 $this->assertTrue( $rev2x->isCurrent() );
612 * @covers Revision::getPrevious
614 public function testGetPrevious() {
615 $oldestRevision = $this->testPage
->getOldestRevision();
616 $latestRevision = $this->testPage
->getLatest();
618 $this->assertNull( $oldestRevision->getPrevious() );
620 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
621 $newRevision = $this->testPage
->getRevision();
623 $this->assertNotNull( $newRevision->getPrevious() );
624 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
628 * @covers Revision::getNext
630 public function testGetNext() {
631 $rev1 = $this->testPage
->getRevision();
633 $this->assertNull( $rev1->getNext() );
635 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
636 $rev2 = $this->testPage
->getRevision();
638 $this->assertNotNull( $rev1->getNext() );
639 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
643 * @covers Revision::newNullRevision
645 public function testNewNullRevision() {
646 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
647 $orig = $this->testPage
->getRevision();
649 $dbw = wfGetDB( DB_MASTER
);
650 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
652 $this->assertNotEquals( $orig->getId(), $rev->getId(),
653 'new null revision should have a different id from the original revision' );
654 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
655 'new null revision should have the same text id as the original revision' );
656 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
657 'new null revision should have the same SHA1 as the original revision' );
658 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
659 'new null revision should have the same content as the original revision' );
660 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
664 * @covers Revision::newNullRevision
666 public function testNewNullRevision_badPage() {
667 $dbw = wfGetDB( DB_MASTER
);
668 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
670 $this->assertNull( $rev );
674 * @covers Revision::insertOn
676 public function testInsertOn() {
677 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
679 $orig = $this->makeRevisionWithProps( [
683 // Make sure the revision was copied to ip_changes
684 $dbr = wfGetDB( DB_REPLICA
);
685 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
686 $row = $res->fetchObject();
688 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
690 $orig->getTimestamp(),
691 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
695 public static function provideUserWasLastToEdit() {
696 yield
'actually the last edit' => [ 3, true ];
697 yield
'not the current edit, but still by this user' => [ 2, true ];
698 yield
'edit by another user' => [ 1, false ];
699 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
703 * @covers Revision::userWasLastToEdit
704 * @dataProvider provideUserWasLastToEdit
706 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
707 $userA = User
::newFromName( "RevisionStorageTest_userA" );
708 $userB = User
::newFromName( "RevisionStorageTest_userB" );
710 if ( $userA->getId() === 0 ) {
711 $userA = User
::createNew( $userA->getName() );
714 if ( $userB->getId() === 0 ) {
715 $userB = User
::createNew( $userB->getName() );
718 $ns = $this->getDefaultWikitextNS();
720 $dbw = wfGetDB( DB_MASTER
);
723 // create revisions -----------------------------
724 $page = WikiPage
::factory( Title
::newFromText(
725 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
726 $page->insertOn( $dbw );
728 $revisions[0] = new Revision( [
729 'page' => $page->getId(),
730 // we need the title to determine the page's default content model
731 'title' => $page->getTitle(),
732 'timestamp' => '20120101000000',
733 'user' => $userA->getId(),
735 'content_model' => CONTENT_MODEL_WIKITEXT
,
736 'comment' => 'edit zero'
738 $revisions[0]->insertOn( $dbw );
740 $revisions[1] = new Revision( [
741 'page' => $page->getId(),
742 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
743 'title' => $page->getTitle(),
744 'timestamp' => '20120101000100',
745 'user' => $userA->getId(),
747 'content_model' => CONTENT_MODEL_WIKITEXT
,
748 'comment' => 'edit one'
750 $revisions[1]->insertOn( $dbw );
752 $revisions[2] = new Revision( [
753 'page' => $page->getId(),
754 'title' => $page->getTitle(),
755 'timestamp' => '20120101000200',
756 'user' => $userB->getId(),
758 'content_model' => CONTENT_MODEL_WIKITEXT
,
759 'comment' => 'edit two'
761 $revisions[2]->insertOn( $dbw );
763 $revisions[3] = new Revision( [
764 'page' => $page->getId(),
765 'title' => $page->getTitle(),
766 'timestamp' => '20120101000300',
767 'user' => $userA->getId(),
769 'content_model' => CONTENT_MODEL_WIKITEXT
,
770 'comment' => 'edit three'
772 $revisions[3]->insertOn( $dbw );
774 $revisions[4] = new Revision( [
775 'page' => $page->getId(),
776 'title' => $page->getTitle(),
777 'timestamp' => '20120101000200',
778 'user' => $userA->getId(),
780 'content_model' => CONTENT_MODEL_WIKITEXT
,
781 'comment' => 'edit four'
783 $revisions[4]->insertOn( $dbw );
785 // test it ---------------------------------
786 $since = $revisions[$sinceIdx]->getTimestamp();
788 $revQuery = Revision
::getQueryInfo();
789 $allRows = iterator_to_array( $dbw->select(
791 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
793 'rev_page' => $page->getId(),
794 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
797 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
801 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
803 $this->assertEquals( $expectedLast, $wasLast );
807 * @param string $text
808 * @param string $title
809 * @param string $model
810 * @param string $format
814 private function newTestRevision( $text, $title = "Test",
815 $model = CONTENT_MODEL_WIKITEXT
, $format = null
817 if ( is_string( $title ) ) {
818 $title = Title
::newFromText( $title );
821 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
829 'content' => $content,
830 'length' => $content->getSize(),
831 'comment' => "testing",
832 'minor_edit' => false,
834 'content_format' => $format,
841 public function provideGetContentModel() {
842 // NOTE: we expect the help namespace to always contain wikitext
844 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
845 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
846 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
851 * @dataProvider provideGetContentModel
852 * @covers Revision::getContentModel
854 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
855 $rev = $this->newTestRevision( $text, $title, $model, $format );
857 $this->assertEquals( $expectedModel, $rev->getContentModel() );
860 public function provideGetContentFormat() {
861 // NOTE: we expect the help namespace to always contain wikitext
863 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
864 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
865 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
866 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
871 * @dataProvider provideGetContentFormat
872 * @covers Revision::getContentFormat
874 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
875 $rev = $this->newTestRevision( $text, $title, $model, $format );
877 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
880 public function provideGetContentHandler() {
881 // NOTE: we expect the help namespace to always contain wikitext
883 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
884 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
885 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
890 * @dataProvider provideGetContentHandler
891 * @covers Revision::getContentHandler
893 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
894 $rev = $this->newTestRevision( $text, $title, $model, $format );
896 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
899 public function provideGetContent() {
900 // NOTE: we expect the help namespace to always contain wikitext
902 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
904 serialize( 'hello world' ),
906 DummyContentForTesting
::MODEL_ID
,
908 Revision
::FOR_PUBLIC
,
909 serialize( 'hello world' )
912 serialize( 'hello world' ),
916 Revision
::FOR_PUBLIC
,
917 serialize( 'hello world' )
923 * @dataProvider provideGetContent
924 * @covers Revision::getContent
926 public function testGetContent( $text, $title, $model, $format,
927 $audience, $expectedSerialization
929 $rev = $this->newTestRevision( $text, $title, $model, $format );
930 $content = $rev->getContent( $audience );
933 $expectedSerialization,
934 is_null( $content ) ?
null : $content->serialize( $format )
939 * @covers Revision::getContent
941 public function testGetContent_failure() {
942 $rev = new Revision( [
943 'page' => $this->testPage
->getId(),
944 'content_model' => $this->testPage
->getContentModel(),
945 'id' => 123456789, // not in the test DB
948 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
950 $this->assertNull( $rev->getContent(),
951 "getContent() should return null if the revision's text blob could not be loaded." );
953 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
954 $this->assertNull( $rev->getContent(),
955 "getContent() should return null if the revision's text blob could not be loaded." );
957 Wikimedia\restoreWarnings
();
960 public function provideGetSize() {
962 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
963 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
968 * @covers Revision::getSize
969 * @dataProvider provideGetSize
971 public function testGetSize( $text, $model, $expected_size ) {
972 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
973 $this->assertEquals( $expected_size, $rev->getSize() );
976 public function provideGetSha1() {
978 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
980 serialize( "hello world." ),
981 DummyContentForTesting
::MODEL_ID
,
982 Revision
::base36Sha1( serialize( "hello world." ) )
988 * @covers Revision::getSha1
989 * @dataProvider provideGetSha1
991 public function testGetSha1( $text, $model, $expected_hash ) {
992 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
993 $this->assertEquals( $expected_hash, $rev->getSha1() );
997 * Tests whether $rev->getContent() returns a clone when needed.
999 * @covers Revision::getContent
1001 public function testGetContentClone() {
1002 $content = new RevisionTestModifyableContent( "foo" );
1004 $rev = new Revision(
1008 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
1010 'content' => $content,
1011 'length' => $content->getSize(),
1012 'comment' => "testing",
1013 'minor_edit' => false,
1017 /** @var RevisionTestModifyableContent $content */
1018 $content = $rev->getContent( Revision
::RAW
);
1019 $content->setText( "bar" );
1021 /** @var RevisionTestModifyableContent $content2 */
1022 $content2 = $rev->getContent( Revision
::RAW
);
1023 // content is mutable, expect clone
1024 $this->assertNotSame( $content, $content2, "expected a clone" );
1025 // clone should contain the original text
1026 $this->assertEquals( "foo", $content2->getText() );
1028 $content2->setText( "bla bla" );
1029 // clones should be independent
1030 $this->assertEquals( "bar", $content->getText() );
1034 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1035 * @covers Revision::getContent
1037 public function testGetContentUncloned() {
1038 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1039 $content = $rev->getContent( Revision
::RAW
);
1040 $content2 = $rev->getContent( Revision
::RAW
);
1042 // for immutable content like wikitext, this should be the same object
1043 $this->assertSame( $content, $content2 );
1047 * @covers Revision::loadFromId
1049 public function testLoadFromId() {
1050 $rev = $this->testPage
->getRevision();
1051 $this->hideDeprecated( 'Revision::loadFromId' );
1052 $this->assertRevEquals(
1054 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1059 * @covers Revision::loadFromPageId
1061 public function testLoadFromPageId() {
1062 $this->assertRevEquals(
1063 $this->testPage
->getRevision(),
1064 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1069 * @covers Revision::loadFromPageId
1071 public function testLoadFromPageIdWithLatestRevId() {
1072 $this->assertRevEquals(
1073 $this->testPage
->getRevision(),
1074 Revision
::loadFromPageId(
1075 wfGetDB( DB_MASTER
),
1076 $this->testPage
->getId(),
1077 $this->testPage
->getLatest()
1083 * @covers Revision::loadFromPageId
1085 public function testLoadFromPageIdWithNotLatestRevId() {
1086 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1087 $this->assertRevEquals(
1088 $this->testPage
->getRevision()->getPrevious(),
1089 Revision
::loadFromPageId(
1090 wfGetDB( DB_MASTER
),
1091 $this->testPage
->getId(),
1092 $this->testPage
->getRevision()->getPrevious()->getId()
1098 * @covers Revision::loadFromTitle
1100 public function testLoadFromTitle() {
1101 $this->assertRevEquals(
1102 $this->testPage
->getRevision(),
1103 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1108 * @covers Revision::loadFromTitle
1110 public function testLoadFromTitleWithLatestRevId() {
1111 $this->assertRevEquals(
1112 $this->testPage
->getRevision(),
1113 Revision
::loadFromTitle(
1114 wfGetDB( DB_MASTER
),
1115 $this->testPage
->getTitle(),
1116 $this->testPage
->getLatest()
1122 * @covers Revision::loadFromTitle
1124 public function testLoadFromTitleWithNotLatestRevId() {
1125 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1126 $this->assertRevEquals(
1127 $this->testPage
->getRevision()->getPrevious(),
1128 Revision
::loadFromTitle(
1129 wfGetDB( DB_MASTER
),
1130 $this->testPage
->getTitle(),
1131 $this->testPage
->getRevision()->getPrevious()->getId()
1137 * @covers Revision::loadFromTimestamp()
1139 public function testLoadFromTimestamp() {
1140 $this->assertRevEquals(
1141 $this->testPage
->getRevision(),
1142 Revision
::loadFromTimestamp(
1143 wfGetDB( DB_MASTER
),
1144 $this->testPage
->getTitle(),
1145 $this->testPage
->getRevision()->getTimestamp()
1151 * @covers Revision::getParentLengths
1153 public function testGetParentLengths_noRevIds() {
1156 Revision
::getParentLengths(
1157 wfGetDB( DB_MASTER
),
1164 * @covers Revision::getParentLengths
1166 public function testGetParentLengths_oneRevId() {
1167 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1168 $textLength = strlen( $text );
1170 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1171 $rev[1] = $this->testPage
->getLatest();
1174 [ $rev[1] => $textLength ],
1175 Revision
::getParentLengths(
1176 wfGetDB( DB_MASTER
),
1183 * @covers Revision::getParentLengths
1185 public function testGetParentLengths_multipleRevIds() {
1186 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1187 $textOneLength = strlen( $textOne );
1188 $textTwo = '831jr091jr092121j09rj1';
1189 $textTwoLength = strlen( $textTwo );
1191 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1192 $rev[1] = $this->testPage
->getLatest();
1193 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1194 $rev[2] = $this->testPage
->getLatest();
1197 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1198 Revision
::getParentLengths(
1199 wfGetDB( DB_MASTER
),
1200 [ $rev[1], $rev[2] ]
1206 * @covers Revision::getTitle
1208 public function testGetTitle_fromExistingRevision() {
1210 $this->testPage
->getTitle()->equals(
1211 $this->testPage
->getRevision()->getTitle()
1217 * @covers Revision::getTitle
1219 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1220 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1222 $this->testPage
->getTitle()->equals(
1229 * @covers Revision::isMinor
1231 public function testIsMinor_true() {
1232 // Use a sysop to ensure we can mark edits as minor
1233 $sysop = $this->getTestSysop()->getUser();
1235 $this->testPage
->doEditContent(
1236 new WikitextContent( __METHOD__
),
1242 $rev = $this->testPage
->getRevision();
1244 $this->assertSame( true, $rev->isMinor() );
1248 * @covers Revision::isMinor
1250 public function testIsMinor_false() {
1251 $this->testPage
->doEditContent(
1252 new WikitextContent( __METHOD__
),
1256 $rev = $this->testPage
->getRevision();
1258 $this->assertSame( false, $rev->isMinor() );
1262 * @covers Revision::getTimestamp
1264 public function testGetTimestamp() {
1265 $testTimestamp = wfTimestampNow();
1267 $this->testPage
->doEditContent(
1268 new WikitextContent( __METHOD__
),
1271 $rev = $this->testPage
->getRevision();
1273 $this->assertInternalType( 'string', $rev->getTimestamp() );
1274 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1275 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1279 * @covers Revision::getUser
1280 * @covers Revision::getUserText
1282 public function testGetUserAndText() {
1283 $sysop = $this->getTestSysop()->getUser();
1285 $this->testPage
->doEditContent(
1286 new WikitextContent( __METHOD__
),
1292 $rev = $this->testPage
->getRevision();
1294 $this->assertSame( $sysop->getId(), $rev->getUser() );
1295 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1299 * @covers Revision::isDeleted
1301 public function testIsDeleted_nothingDeleted() {
1302 $rev = $this->testPage
->getRevision();
1304 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1305 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1306 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1307 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1311 * @covers Revision::getVisibility
1313 public function testGetVisibility_nothingDeleted() {
1314 $rev = $this->testPage
->getRevision();
1316 $this->assertSame( 0, $rev->getVisibility() );
1320 * @covers Revision::getComment
1322 public function testGetComment_notDeleted() {
1323 $expectedSummary = 'goatlicious summary';
1325 $this->testPage
->doEditContent(
1326 new WikitextContent( __METHOD__
),
1329 $rev = $this->testPage
->getRevision();
1331 $this->assertSame( $expectedSummary, $rev->getComment() );
1335 * @covers Revision::isUnpatrolled
1337 public function testIsUnpatrolled_returnsRecentChangesId() {
1338 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1339 $rev = $this->testPage
->getRevision();
1341 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1342 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1346 * @covers Revision::isUnpatrolled
1348 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1349 // This assumes that sysops are auto patrolled
1350 $sysop = $this->getTestSysop()->getUser();
1351 $this->testPage
->doEditContent(
1352 new WikitextContent( __METHOD__
),
1358 $rev = $this->testPage
->getRevision();
1360 $this->assertSame( 0, $rev->isUnpatrolled() );
1364 * This is a simple blanket test for all simple content getters and is methods to provide some
1365 * coverage before the split of Revision into multiple classes for MCR work.
1366 * @covers Revision::getContent
1367 * @covers Revision::getSerializedData
1368 * @covers Revision::getContentModel
1369 * @covers Revision::getContentFormat
1370 * @covers Revision::getContentHandler
1372 public function testSimpleContentGetters() {
1373 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1374 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1376 $this->testPage
->doEditContent(
1377 new WikitextContent( $expectedText ),
1380 $rev = $this->testPage
->getRevision();
1382 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1383 $this->assertSame( $expectedText, $rev->getSerializedData() );
1384 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1385 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1386 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1390 * @covers Revision::newKnownCurrent
1392 public function testNewKnownCurrent() {
1393 // Setup the services
1394 $this->overrideMwServices();
1395 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1396 $this->setService( 'MainWANObjectCache', $cache );
1397 $db = wfGetDB( DB_MASTER
);
1399 // Get a fresh revision to use during testing
1400 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1401 $rev = $this->testPage
->getRevision();
1403 // Clear any previous cache for the revision during creation
1404 $key = $cache->makeGlobalKey(
1405 RevisionStore
::ROW_CACHE_KEY
,
1410 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1411 $this->assertFalse( $cache->get( $key ) );
1413 // Get the new revision and make sure it is in the cache and correct
1414 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1415 $this->assertRevEquals( $rev, $newRev );
1417 $cachedRow = $cache->get( $key );
1418 $this->assertNotFalse( $cachedRow );
1419 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1422 public function testNewKnownCurrent_withPageId() {
1423 $db = wfGetDB( DB_MASTER
);
1425 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1426 $rev = $this->testPage
->getRevision();
1428 $pageId = $this->testPage
->getId();
1430 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1431 $this->assertRevEquals( $rev, $newRev );
1434 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1435 $db = wfGetDB( DB_MASTER
);
1437 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1440 public function provideUserCanBitfield() {
1441 yield
[ 0, 0, [], null, true ];
1442 // Bitfields match, user has no permissions
1443 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1444 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1445 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1446 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1447 // Bitfields match, user (admin) does have permissions
1448 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1449 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1450 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1451 // Bitfields match, user (admin) does not have permissions
1452 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1453 // Bitfields match, user (oversight) does have permissions
1454 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1455 // Check permissions using the title
1457 Revision
::DELETED_TEXT
,
1458 Revision
::DELETED_TEXT
,
1464 Revision
::DELETED_TEXT
,
1465 Revision
::DELETED_TEXT
,
1473 * @dataProvider provideUserCanBitfield
1474 * @covers Revision::userCanBitfield
1476 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1477 $title = Title
::newFromText( $title );
1479 $this->setMwGlobals(
1480 'wgGroupPermissions',
1483 'deletedtext' => true,
1484 'deletedhistory' => true,
1487 'viewsuppressed' => true,
1488 'suppressrevision' => true,
1492 $user = $this->getTestUser( $userGroups )->getUser();
1496 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1499 // Fallback to $wgUser
1500 $this->setMwGlobals(
1506 Revision
::userCanBitfield( $bitField, $field, null, $title )
1510 public function provideUserCan() {
1511 yield
[ 0, 0, [], true ];
1512 // Bitfields match, user has no permissions
1513 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1514 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1515 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1516 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1517 // Bitfields match, user (admin) does have permissions
1518 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1519 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1520 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1521 // Bitfields match, user (admin) does not have permissions
1522 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1523 // Bitfields match, user (oversight) does have permissions
1524 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1528 * @dataProvider provideUserCan
1529 * @covers Revision::userCan
1531 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1532 $this->setMwGlobals(
1533 'wgGroupPermissions',
1536 'deletedtext' => true,
1537 'deletedhistory' => true,
1540 'viewsuppressed' => true,
1541 'suppressrevision' => true,
1545 $user = $this->getTestUser( $userGroups )->getUser();
1546 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1550 $revision->userCan( $field, $user )
1554 public function provideGetTextId() {
1557 $slot = new SlotRecord( (object)[
1558 'slot_revision_id' => 42,
1559 'slot_content_id' => 1,
1560 'content_address' => 'tt:789',
1561 'model_name' => CONTENT_MODEL_WIKITEXT
,
1562 'role_name' => SlotRecord
::MAIN
,
1564 ], new WikitextContent( 'Test' ) );
1566 $rec = new MutableRevisionRecord( $this->testPage
->getTitle() );
1568 $rec->setSlot( $slot );
1570 yield
[ $rec, 789 ];
1574 * @dataProvider provideGetTextId
1575 * @covers Revision::getTextId()
1577 public function testGetTextId( $spec, $expected ) {
1578 $rev = new Revision( $spec, 0, $this->testPage
->getTitle() );
1579 $this->assertSame( $expected, $rev->getTextId() );
1582 public function provideGetRevisionText() {
1589 * @dataProvider provideGetRevisionText
1590 * @covers Revision::getRevisionText
1592 public function testGetRevisionText( array $queryInfoOptions, array $queryInfoExtra = [] ) {
1593 $rev = $this->testPage
->getRevisionRecord();
1595 $queryInfo = Revision
::getQueryInfo( $queryInfoOptions );
1596 $queryInfo['tables'] = array_merge( $queryInfo['tables'], $queryInfoExtra['tables'] ??
[] );
1597 $queryInfo['fields'] = array_merge( $queryInfo['fields'], $queryInfoExtra['fields'] ??
[] );
1598 $queryInfo['joins'] = array_merge( $queryInfo['joins'], $queryInfoExtra['joins'] ??
[] );
1600 $conds = [ 'rev_id' => $rev->getId() ];
1601 $row = $this->db
->selectRow(
1602 $queryInfo['tables'],
1603 $queryInfo['fields'],
1610 $expected = $rev->getContent( SlotRecord
::MAIN
)->serialize();
1612 $this->hideDeprecated( 'Revision::getRevisionText (MCR without SCHEMA_COMPAT_WRITE_OLD)' );
1613 $this->assertSame( $expected, Revision
::getRevisionText( $row ) );